package com.cty.tree;

/*
*
* 顺序存储二叉树
*
*
* */
public class ArrBinaryTree {
    int[] arr;

    public ArrBinaryTree(int[] arr) {
        this.arr = arr;
    }

    //由于调用方法要传入参数0所以重载一下好看些
    public void preOrder() {
        this.preOrder(0);
    }

    public void midOrder() {
        this.midOrder(0);
    }

    public void postOrder() {
        this.postOrder(0);
    }

    //前序遍历二叉树
    public void preOrder(int index) {
        if (arr == null && arr.length == 0) {
            System.out.println("空树，不能遍历哦~");
            return;
        }
        System.out.println(arr[index]);
        if ((2 * index + 1) < arr.length) {
            preOrder(2 * index + 1);
        }
        if ((2 * index + 2) < arr.length) {
            preOrder(2 * index + 2);
        }
    }

    //中序遍历二叉树
    public void midOrder(int index) {
        if (arr.length == 0 && arr == null) {
            System.out.println("空树，不能遍历哦~");
            return;
        }
        if ((2 * index + 1) < arr.length) {
            midOrder(2 * index + 1);
        }
        System.out.println(arr[index]);
        if ((2 * index + 2) < arr.length) {
            midOrder(2 * index + 2);
        }
    }

    //后序遍历二叉树
    public void postOrder(int index) {
        if (arr.length == 0 && arr == null) {
            System.out.println("空树，不能遍历哦~");
            return;
        }
        if ((2 * index + 1) < arr.length) {
            postOrder(2 * index + 1);
        }
        if ((2 * index + 2) < arr.length) {
            postOrder(2 * index + 2);
        }
        System.out.println(arr[index]);
    }
}
